import 'dart:io';

import 'package:cbec_app/components/base/base_ctr.dart';
import 'package:cbec_app/components/http/network.dart';
import 'package:cbec_app/utils/OssUpload.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:umeng_common_sdk/umeng_common_sdk.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

import '../../../entity/goods_center/clasify_item_entity.dart';
import '../../../entity/goods_center/comment_file_entity.dart';
import '../../../utils/EventMgr.dart';

///评论控制器
class CommentController extends BaseController {
  final TextEditingController contentController = TextEditingController();

  int count = 0; //字数

  String commentTxt = '好评';

  double mainStar = 5.0;
  double spfhdStar = 5.0;
  double djfwStar = 5.0;
  double kdpsStar = 5.0;
  double kdyfwStar = 5.0;
  double kdbzStar = 5.0;

  late String orderID = '';
  late String channelID = '';
  late String paySn = '';
  late String? sn = '';

  List<AssetEntity> imageList = [];

  List<String>? imageUrlList = [];
  List<CommentFileEntity> imageUrlMap = [];

  @override
  void onInit() {
    print(Get.arguments);
    if (Get.arguments != '') {
      orderID = Get.arguments['orderID'];
      channelID = Get.arguments['channelID'];
      paySn = Get.arguments['paySn'];
      sn = Get.arguments['sn'];
    } else {
      Tools.showToast("参数错误");
      Get.back();
    }

    super.onInit();

    UmengCommonSdk.onPageStart('评价');
  }

  @override
  void onClose() {
    super.onClose();
    UmengCommonSdk.onPageEnd('评价');
  }

  Future<void> submit() async {
    Tools.showLoading();
    if (imageList!.isNotEmpty) {
      List<File> fileList = [];
      imageList!.forEach((element) async {
        File? file = await element.file;
        fileList.add(file!);
      });

      imageUrlList = await OssUpload.upoladFiles(fileList);
      imageUrlList!.forEach((v) {
        if (v.contains('mp4')) {
          imageUrlMap.add(CommentFileEntity(v, '2'));
        } else {
          imageUrlMap.add(CommentFileEntity(v, '1'));
        }
      });
    }

    await Network.comment(channelID, orderID, paySn, sn ?? '', spfhdStar, djfwStar, kdbzStar, kdpsStar, kdyfwStar, mainStar, contentController.text, imageUrlMap!).then((value) {
      // eventBus.fire(MyOrderEventBus('121213'));
      Tools.showToast("评价完成");
      EventMgr.send(EventType.refreshCommentList);
      EventMgr.send(EventType.refreshOrderList);
      EventMgr.send(EventType.refreshLogin);
    }).catchError((onError) {
      print("评价错误:${onError}");
      Tools.showToast("评价错误");
    });
    Tools.hideLoading();
    Get.back();
    // Tools.showModal(content: "总评分:${mainStar}\n商品服务:${spfhdStar}\n店家服务:${djfwStar}\n快递配送:${kdpsStar}\n快递员服务:${kdyfwStar}\n快递包装:${kdbzStar}");
  }
}
